package com.aliyun.heiyu.powermaster.server.strategy;

import com.aliyun.heiyu.powermaster.server.factory.MasterThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class StrategyExecutor {
    private static Logger logger = LoggerFactory.getLogger(StrategyExecutor.class);

    /** 只供Strategy相关逻辑使用 */
    private ScheduledThreadPoolExecutor scheduledThreadPool;
    private ThreadPoolExecutor threadPool;

    private static volatile StrategyExecutor instance;

    public static StrategyExecutor getInstance(){
            synchronized (StrategyExecutor.class){
                if(instance == null){
                    instance = new StrategyExecutor();
                }
            }

        return instance;
    }

    private StrategyExecutor(){
        scheduledThreadPool = new ScheduledThreadPoolExecutor(3, new MasterThreadFactory("Strategy"));
        threadPool = new ThreadPoolExecutor(3,3,0,TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1),new MasterThreadFactory("Strategy"));
    }
 
    /**
     * 注意：ScheduledThreadPoolExecutor会吞异常的
     * 建议需要严格按照时间去执行的情况下选择该线程池
     * */
    public ScheduledThreadPoolExecutor getScheduledThreadPool(){
        return this.scheduledThreadPool;
    }

    public ThreadPoolExecutor getThreadPool(){
        return this.threadPool;
    }
}
